package com.example.tianling.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.R;
import com.example.tianling.common.vo.ArticleTrainCommentVo;
import com.example.tianling.common.vo.ArticleVo;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.ls.LSException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Login
    @PostMapping(value="/book/searchBook.do")
    @ResponseBody
    public R searchBooks(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String keyword = paramsJSONObject.getString("keyword");
        String page = paramsJSONObject.getString("page");
        return R.success(bookService.searchBooks(keyword, Integer.valueOf(page)));
    }

    @Login
    @PostMapping(value="/book/getThemeAreaBooks.do")
    @ResponseBody
    public R getThemeAreaBooks(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        System.out.println(paramsJSONObject);
        String area = paramsJSONObject.getString("area");
        String type = paramsJSONObject.getString("type");
        String describe = paramsJSONObject.getString("describe");
        String page = paramsJSONObject.getString("page");
        String keyword = paramsJSONObject.getString("keyword");
        System.out.println(bookService.getBooksByAreaAndTheme(area,type,describe, Integer.valueOf(page),keyword));
        return R.success(bookService.getBooksByAreaAndTheme(area,type,describe, Integer.valueOf(page),keyword));
    }

    @PostMapping(value = "/book/order_book")
    @ResponseBody
    public R orderBook() {
        // 结构化：地区
        bookService.orderBook();
        return R.success();
    }
}
